/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-21 09:31:31
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-05-11 12:33:18
 * @FilePath: \cxl-driving-training\src\views\drivingSchoolManagement\coach\edit\index.ts
 */
import { fileProps, formDataInterface, PropsFormpData } from '@/utils/interface'
import { reactive, ref } from 'vue'
import { listConfig } from './formConfig'
import { coachAdd, coachDetailed, coachEdit, coachOperationInterface } from '@/apis/coach'
import { ElMessage } from 'element-plus'
import router from '@/router'
import { routerStore } from '@/store/router.store'
import { useRoute } from 'vue-router'
import { userStore } from '@/store/user.store'
import { addDisplayBlockToImages } from '@/utils'
export default class CoachOperation {
    formData = reactive<PropsFormpData[]>([...listConfig])
    //编辑类型 create | edit | look
    operation = reactive({
        type: 'create',
        id: ''
    })
    routerStore = routerStore()
    uStore = userStore()
    constructor() {
        const route = useRoute()
        this.operation.type = route.query.type as string
        this.operation.id = route.query?.id as string
        // ;(this.formData[1][0].options as any[]).length = 0

        const options: { label: string; value: string }[] = []
        this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
            res.forEach(item => {
                options.push({ label: item.shortName, value: item.id })
            })

            this.formData.forEach((item: PropsFormpData, index: number) => {
                if (Object.prototype.toString.call(item) === '[object Array]' && index === 4) {
                    const childList = item as formDataInterface[]
                    childList[0].options = options
                    console.log('childList', childList)
                }
            })
        })

        if (this.operation.type === 'create') {
            //初始化数据
        } else {
            this.getData()
        }
    }
    getData = (arg?: any) => {
        coachDetailed(this.operation.id).then(res => {
            if (Number(res.data.code) === 200) {
                const result = res.data.result
                this.formData.forEach((child: PropsFormpData) => {
                    if (Object.prototype.toString.call(child) === '[object Array]') {
                        const childList = child as formDataInterface[]
                        childList.forEach((item: formDataInterface) => {
                            if (item.key === 'teachAge') {
                                item.value = result.teachAge + ''
                            } else if (item.key === 'introduce') {
                                item.value = result.introduce + ''
                                console.log('result.introduce ', result.introduce)
                            } else if (item.key === 'name') {
                                item.value = result.name + ''
                            } else if (item.key === 'phone') {
                                item.value = result.phone + ''
                            } else if (item.key === 'schoolId') {
                                item.value = result.schoolId + ''
                            } else if (item.key === 'sex') {
                                item.value = result.sex + ''
                            } else if (item.key === 'headPic') {
                                if (result.headPic) {
                                    item.value = [
                                        {
                                            index: 0,
                                            tag: '',
                                            type: 'image',
                                            list: [
                                                {
                                                    key: result.headPic,
                                                    name: ''
                                                }
                                            ]
                                        }
                                    ]
                                }
                            }
                        })
                    }
                })
            }
        })
    }
    //取消创建
    onCancel = () => {
        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
        router.push('/coach')
    }
    //保存
    onSave = (formRef: any) => {
        if (formRef?.validateForm()) {
            const data: coachOperationInterface = {
                headPic: '',
                introduce: '',
                name: '',
                phone: '',
                schoolId: '',
                sex: 0,
                teachAge: 0
            }

            this.formData.forEach((child: PropsFormpData) => {
                if (Object.prototype.toString.call(child) === '[object Array]') {
                    const childList = child as formDataInterface[]
                    childList.forEach((item: formDataInterface) => {
                        console.log(item)
                        if (item.key === 'name') {
                            data.name = item.value as string
                        } else if (item.key === 'teachAge') {
                            data.teachAge = item.value as number
                        } else if (item.key === 'sex') {
                            data.sex = item.value as number
                        } else if (item.key === 'schoolId') {
                            data.schoolId = item.value as string
                        } else if (item.key === 'phone') {
                            data.phone = item.value as string
                        } else if (item.key === 'introduce') {
                            data.introduce = addDisplayBlockToImages(item.value as string)
                        } else if (item.key === 'headPic') {
                            const logo: fileProps[] = item.value as fileProps[]
                            data[item.key] = logo[0].list[0].key
                        }
                    })
                }
            })

            if (this.operation.type === 'create') {
                coachAdd(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('新增成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/coach')
                    }
                })
            } else {
                data.id = this.operation.id
                coachEdit(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('修改成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/coach')
                    }
                })
            }
        }
    }
}
